﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using SMS.WinForms;

namespace SMS.Console.Controls
{
    public partial class CommConfigPage : ConfigPage
    {
        public CommConfigPage()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            XmlDocument xDoc = base.Document;

            var portName = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='PortName']");
            var baudRate = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='BaudRate']");
            var dataBits = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='DataBits']");
            var parity = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='Parity']");
            var stopBits = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='StopBits']");
            var rtsEnable = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='RtsEnable']");
            var dtrEnable = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='DtrEnable']");

            if (portName != null) cbPortName.Text = portName.Attributes["value"].Value;
            if (baudRate != null) cbBaudRate.Text = baudRate.Attributes["value"].Value;
            if (dataBits != null) cbDataBits.Text = dataBits.Attributes["value"].Value;
            if (parity != null) cbParity.Text = parity.Attributes["value"].Value;
            if (stopBits != null) cbStopBits.Text = stopBits.Attributes["value"].Value;
            if (rtsEnable != null) chkRtsEnable.Checked = bool.Parse(rtsEnable.Attributes["value"].Value);
            if (dtrEnable != null) chkDtrEnable.Checked = bool.Parse(dtrEnable.Attributes["value"].Value);
        }

        public override bool CanNavigate(SMS.WinForms.WizardFormNavigationEventArgs e)
        {
            try
            {
                if (e.NavigationType == SMS.WinForms.WizardFormNavigationTypes.Next)
                {
                    XmlDocument xDoc = base.Document;
                    var portName = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='PortName']");
                    var baudRate = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='BaudRate']");
                    var dataBits = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='DataBits']");
                    var parity = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='Parity']");
                    var stopBits = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='StopBits']");
                    var rtsEnable = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='RtsEnable']");
                    var dtrEnable = xDoc.SelectSingleNode("/configuration/appSettings/add[@key='DtrEnable']");

                    portName.Attributes["value"].Value = cbPortName.Text;
                    baudRate.Attributes["value"].Value = cbBaudRate.Text;
                    dataBits.Attributes["value"].Value = cbDataBits.Text;
                    parity.Attributes["value"].Value = cbParity.Text;
                    stopBits.Attributes["value"].Value = cbStopBits.Text;
                    rtsEnable.Attributes["value"].Value = chkRtsEnable.Checked.ToString();
                    dtrEnable.Attributes["value"].Value = chkDtrEnable.Checked.ToString();
                }
            }
            catch (Exception ex)
            {
                ErrorBox.Show(ex);
                return false;
            }
            return true;
        }
    }
}
